package models

import (
	"ShopSupplierApi/models/mysql"
	"bytes"
	"strconv"
	"time"
)

//ConditionDiscountData 优惠活动
type ConditionDiscountData struct{}

//Discount 优惠信息
type Discount struct {
	Params      []interface{} //优惠参数
	Name        string        //优惠名称
	SupplierID  uint64        //供应商ID
	SelectIndex int           //优惠类型编号
}

//Condition 条件信息
type Condition struct {
	SupplierID uint64  //供应商ID
	Name       string  //优惠名称
	Value      string  //相关产品或分类ID集合，用，分割
	ValueType  string  //value类型【"cat"=分类，"product"=商品】
	nums       int     //数量限制
	Wlow       float64 //最小重量
	Whigh      float64 //最大重量
	Mlow       float64 //最小金额
	Mhigh      float64 //最大金额
}

//ConditionDiscount 优惠活动数据
type ConditionDiscount struct {
	ConditionID int64     //条件ID
	DiscountID  int64     //优惠ID
	Name        string    //活动名称
	Sort        int       // 排序
	CdType      int       //活动类型
	Begin       time.Time //开始时间
	End         time.Time //结束时间
	SupplierID  uint64    //供应商ID
}

//ConDis 优惠活动实例
var ConDis *ConditionDiscountData

func init() {
	ConDis = &ConditionDiscountData{}
}

//ProductList 货品列表
//param supplierID  uint64 供应商ID
//param p *Page 数据分页信息
func (c *ConditionDiscountData) ProductList(supplierID uint64, p *Page) []map[string]interface{} {
	var result []map[string]interface{}
	sql := " SELECT product_id id,product_name name FROM lcf_supplier_products WHERE supplier_id = ? "
	if supplierID == 0 {
		sql = "SELECT product_id id,name,spec_info FROM sdb_b2c_products WHERE 0 = ? "
	}
	result, err := mysql.Rows(p.SQL(sql), supplierID)
	if err != nil {
		return result
	}
	for _, v := range result { //处理货品信息
		v["id"], _ = mysql.Uint64(v["id"])
		v["spec_info"], _ = mysql.String(v["spec_info"])
		v["name"], _ = mysql.String(v["name"])
		if v["spec_info"].(string) != "" {
			v["name"] = v["name"].(string) + " - " + v["spec_info"].(string)
		}
	}
	p.Rows = int64(len(result))
	p.CountRows(sql, supplierID)
	return result
}

//CatList 商品分类列表
//param p *Page 数据分页信息
func CatList(p *Page) []map[string]interface{} {
	var result []map[string]interface{}
	sql := " SELECT cat_id id,cat_name name FROM sdb_b2c_goods_cat "
	result, err := mysql.Rows(p.SQL(sql))
	if err != nil {
		return result
	}
	for _, v := range result {
		v["id"], _ = mysql.Int(v["id"])
		v["name"], _ = mysql.String(v["name"])
	}
	p.Rows = int64(len(result))
	p.CountRows(sql)
	return result
}

//DiscountOrConditionList 获取优惠或条件列表
//param supplierID uint64 供应商ID
//param isCondition bool 是否是条件
//param p *Page 数据分页信息
func (c *ConditionDiscountData) DiscountOrConditionList(supplierID uint64, isCondition bool, p *Page) []map[string]interface{} {
	sql := " SELECT discount_id id,discount_name name FROM lcf_discount "
	if isCondition {
		sql = " SELECT condition_id id,condition_name name FROM lcf_condition "
	}
	sql += " WHERE supplier_id = ? "
	result, err := mysql.Rows(sql, supplierID)
	if err != nil {
		return result
	}
	for _, v := range result {
		v["id"], _ = mysql.Int(v["id"])
		v["name"], _ = mysql.String(v["name"])
	}
	p.Rows = int64(len(result))
	p.CountRows(sql, supplierID)
	return result
}

//ConditionDiscountList 优惠活动列表
//param supplierID uint64 供应商ID
//param p *Page 数据分页信息
func (c *ConditionDiscountData) ConditionDiscountList(supplierID uint64, p *Page) []map[string]interface{} {
	var result []map[string]interface{}
	sql := "SELECT condition_discount_id,b.condition_name,c.discount_name,a.`begin`,a.`end`,a.disabled,a.cdtype,a.sort,a.cdname FROM lcf_condition_discount a LEFT JOIN lcf_condition b ON a.condition_id = b.condition_id " +
		" LEFT JOIN lcf_discount c ON a.discount_id = c.discount_id " +
		" WHERE a.supplier_id = ?"
	result, err := mysql.Rows(p.SQL(sql), supplierID)
	if err != nil {
		return result
	}
	for _, v := range result {
		v["condition_discount_id"], _ = mysql.Int(v["condition_discount_id"])
		v["condition_name"], _ = mysql.String(v["condition_name"])
		v["discount_name"], _ = mysql.String(v["discount_name"])
		v["begin"], _ = mysql.String(v["begin"])
		v["end"], _ = mysql.String(v["end"])
		v["disabled"], _ = mysql.Int(v["disabled"])
		v["cdtype"], _ = mysql.Int(v["cdtype"])
		v["sort"], _ = mysql.Int(v["sort"])
		v["cdname"], _ = mysql.String(v["cdname"])
	}
	p.Rows = int64(len(result))
	p.CountRows(sql, supplierID)
	return result
}

//AddDiscount 添加优惠信息
//param discount Discount 优惠信息
func (c *ConditionDiscountData) AddDiscount(discount Discount) bool {
	if discount.Params == nil {
		discount.Params = make([]interface{}, 0)
	}
	var sql, vsql bytes.Buffer
	args := []interface{}{discount.SupplierID, discount.Name, discount.SelectIndex}
	sql.WriteString("INSERT INTO lcf_discount(supplier_id,discount_name,selectindex")
	vsql.WriteString(")VALUES(?,?,?")
	for index, v := range discount.Params {
		sql.WriteString(",p")
		sql.WriteString(strconv.Itoa(index))
		vsql.WriteString(",?")
		args = append(args, v)
	}
	sql.WriteString(")")
	sql.Write(vsql.Bytes())
	_, err := mysql.Exec(sql.String(), args...)
	return err == nil
}

//AddCondition 添加优惠条件
//param condition Condition 优惠数据
func (c *ConditionDiscountData) AddCondition(condition Condition) bool {
	sql := "INSERT INTO lcf_condition(supplier_id,condition_name,value,value_type,nums,weight_low,weight_high,money_low,money_high)VALUES(" +
		"?,?,?,?,?,?,?,?,?)"
	_, err := mysql.Exec(sql, condition.SupplierID, condition.Name, condition.Value, condition.ValueType, condition.nums,
		condition.Wlow, condition.Whigh, condition.Mlow, condition.Mhigh)
	return err == nil
}

//AddConditionDiscout 添加优惠活动
//param cd ConditionDiscount 优惠活动数据
func (c *ConditionDiscountData) AddConditionDiscout(cd ConditionDiscount) bool {
	sql := "INSERT INTO lcf_condition_discount(discount_id,condition_id,begin,end,sort,cdname,cdtype,createtime,supplier_id)" +
		"VALUES(?,?,?,?,?,?,?,?,? )"
	_, err := mysql.Exec(sql, cd.DiscountID, cd.ConditionID, cd.Begin, cd.End, cd.Sort, cd.Name, cd.CdType, time.Now(), cd.SupplierID)
	return err == nil
}
